﻿Public Class UC_QuanLyDichVu
    Sub ChenHinhanh(ByVal bt As Button)
        bt.Image = Image.FromFile("D:\Visual Studio Projects\MMT2\DXWindowsApplication5\DXWindowsApplication5\Resources\table.png")
        bt.ImageAlign = ContentAlignment.MiddleRight
        bt.TextAlign = ContentAlignment.MiddleLeft
        bt.FlatStyle = FlatStyle.Flat
    End Sub
    Dim makh As Integer = 0

    Public Sub receiveDataTKKhachHang(ByVal kh As DTOKhachHang)
       
    End Sub
    ' Tinh trang: 1 trong, 2 dang su dung, 3 Da dat ban
    Dim tinhtrang As Integer = 1
    Dim tb As New DTOBanAn
    Function ds() As List(Of DTOBanAn)
        Dim dsba As New List(Of DTOBanAn)
        Dim dt As DataTable = BUSBanAn.BanAn_Load()
        Dim dr As DataRow
        For i As Integer = 0 To dt.Rows.Count - 1
            dr = dt.Rows(i)
            Dim x As New DTOBanAn
            x.MaBan = dr("Maban")
            x.MaTT = dr("Matt")
            x.TenBan = dr("Tenban")
            dsba.Add(x)
        Next
        Return dsba
    End Function
    Dim dsBanAn As List(Of DTOBanAn) = ds()
    Sub Hienthibanan()

        Dim dem As Integer = 0
        Dim s As Integer = 1
        Dim tongsobanghe As Integer = dsBanAn.Count
        For Each iteam As DTOBanAn In dsBanAn
            Dim bt As New Button
            'Mac dinh x=20 y = 100
            If dem > 0 And dem Mod 4 = 0 Then
                s = s + 1
                bt.Location = New System.Drawing.Point(0, s * 90)
                dem = 0
            Else
                bt.Location = New System.Drawing.Point(dem * 90, s * 90)
            End If
            bt.Size = New System.Drawing.Size(72, 70)
            bt.Text = iteam.TenBan
            bt.Name = iteam.MaBan
            AddHandler bt.Click, New EventHandler(AddressOf test_click)
            ChenHinhanh(bt)
            If iteam.MaTT = 1 Then
                bt.BackColor = Color.Silver
            ElseIf iteam.MaTT = 2 Then
                bt.BackColor = Color.Orange
            ElseIf iteam.MaTT = 3 Then
                bt.BackColor = Color.HotPink
            End If
            pnBanan.Controls.Add(bt)
            dem = dem + 1
        Next
        'For i As Integer = 1 To 4
        '    For j As Integer = 1 To tongsobanghe \ 4 + 1
        '        If dem > tongsobanghe Then
        '            Return
        '        End If
        '        Dim tenBan As String = dsBanAn(dem).TenBan.ToString
        '        Dim trangThai As Integer = Convert.ToInt16(dsBanAn(dem).MaTT)
        '        Dim bt As New Button
        '        bt.Name = "btnBan" & dsBanAn(dem).MaBan.ToString
        '        bt.Text = tenBan
        '        bt.Location = New System.Drawing.Point(80 * j, i * 90)
        '        bt.Size = New System.Drawing.Size(70, 70)
        '        ChenHinhanh(bt)
        '        If i Mod 2 = 0 Then
        '            bt.BackColor = Color.Silver
        '        Else
        '            bt.BackColor = Color.Orange
        '        End If
        '        pnBanan.Controls.Add(bt)
        '        dem = dem + 1
        '    Next
        'Next
    End Sub

    Private Sub test_click(sender As Object, e As EventArgs)
        Dim b As Button = DirectCast(sender, Button)
        'b.BackColor = Color.Snow
        For Each i As DTOBanAn In dsBanAn
            If i.MaBan = b.Name Then
                tb.MaBan = i.MaBan
                tb.MaTT = i.MaTT
                tb.TenBan = i.TenBan
                Return
            End If
        Next
    End Sub
    Private Sub btnThanhtoan_Click(sender As Object, e As EventArgs) Handles btnThanhtoan.Click
        If AgreeForm.Caution = False Then
            Return
        End If
        Dim giamgia As Integer = Convert.ToInt16(txtGiamgia.Text)
        If giamgia > 100 Or giamgia < 0 Then
            MsgBox("Chỉ được nhập từ 1->100")
            Return
        End If
        hd.MaHD = LayMaHDTheoBanSelect(tb.MaBan)
        Dim sqlSum As String = "select SUM(ThanhTien) from CTHoaDon where MaHD = " & hd.MaHD
        Dim tienan As Double = Convert.ToDouble(DataProvider.ExecuteScalar(sqlSum))
        Dim tongtien As Double = tienan * (100 - giamgia) / 100
        Dim sqledit As String = String.Format("update HOADON set TinhTrang = 1, TongTien = {0} where MaHD = {1}", tongtien, hd.MaHD)
        Dim sqlban As String = "update BanAn set MaTT = 1 where maban = " & tb.MaBan
        DataProvider.ExecuteNonQuery(sqlban)
        gcHoadon.ShowPrintPreview()
        loadBanAn()
        txtTienan.Text = tienan
        txtTongCong.Text = tongtien
        DataProvider.ExecuteNonQuery(sqledit)

        'Dim f As New F_ThanhToan
        'f.ShowDialog()
    End Sub

    Private Sub btnTimKiem_Click_1(sender As Object, e As EventArgs) Handles btnTimKiem.Click
        Dim f As New F_TimKiemKhachHang
        f.ShowDialog()
        txtTenkh.Text = f.Tenkh
        makh = f.Makh
    End Sub
    Sub loadBanAn()
        Dim sql As String = "select MaBan,TenBan,TenTT,t.MaTT from BANAN b, TINHTRANG t where b.MaTT = t.MaTT"
        gvBanan.DataSource = DataProvider.LoadTable(sql)
    End Sub
    Private Sub UC_QuanLyDichVu_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        'Hienthibanan()
        loadBanAn()
        loadMonAn()
        txtNgay.Text = Date.Now
    End Sub

    Private Sub btnHuyHoaDOn_Click(sender As Object, e As EventArgs) Handles btnHuyHoaDOn.Click

        If MsgBox("Bạn có chắc là hủy bàn này không ", MsgBoxStyle.YesNo) = MsgBoxResult.No Then
            Return
        End If
        tb.MaTT = 1
        tb.MaBan = gvBanan.CurrentRow.Cells("MaBan").Value
        tb.TenBan = gvBanan.CurrentRow.Cells("TenBan").Value
        BUSBanAn.BanAn_Update(tb)
        loadBanAn()
    End Sub

    Private Sub Panel1_Paint(sender As Object, e As PaintEventArgs)

    End Sub

    Private Sub btnDatbantruoc_Click(sender As Object, e As EventArgs) Handles btnDatbantruoc.Click
        If tb.MaTT = 3 Or tb.MaTT = 2 Then
            MsgBox("Bàn này đã được đặt rồi hoặc đã sử dụng, ban vui lòng mở bàn khác")
            Return
        End If
        If MsgBox("Bạn có chắc là đặt trước bàn này không ", MsgBoxStyle.YesNo) = MsgBoxResult.No Then
            Return
        End If
        getDataInGvBanAn()
        tb.MaTT = 3
        BUSBanAn.BanAn_Update(tb)
        loadBanAn()
    End Sub
    Dim hd As New DTOHoaDon
    Dim cthd As New DTOCTHoaDon
    dim monan As New DTOMonAn
    Private Sub btnMoban_Click(sender As Object, e As EventArgs) Handles btnMoban.Click
        If tb.MaTT = 2 Then
            MsgBox("Bàn này đã được sử dụng, ban vui lòng mở bàn khác")
            Return
        End If
        If MsgBox("Bạn có chắc là mở bàn này không ", MsgBoxStyle.YesNo) = MsgBoxResult.No Then
            Return
        End If
        If txtTenkh.Text = "" Then
            MsgBox("Tên khách hàng không được để trống!")
            Return
        End If
        getDataInGvBanAn()
        tb.MaTT = 2
        BUSBanAn.BanAn_Update(tb)
        '---------Tao hoa don
        hd.NgayXuat = Convert.ToDateTime(txtNgay.Text)
        hd.MaKH = makh
        hd.TenKH = txtTenkh.Text
        hd.MaKH = makh
        hd.TenBan = tb.TenBan
        hd.MaBan = tb.MaBan
        hd.TongTien = 0
        hd.MaNV = Account.MaNV
        hd.Tinhtrang = 0
        BUSHoaDOn.HoaDOn_Insert(hd)
        loadBanAn()
    End Sub
    Sub getDataInGvBanAn()
        tb.MaTT = gvBanan.CurrentRow.Cells("MaTT").Value
        tb.MaBan = gvBanan.CurrentRow.Cells("MaBan").Value
        tb.TenBan = gvBanan.CurrentRow.Cells("TenBan").Value
    End Sub
    Sub loadCTHoaDonTheoMaHD(ByVal _maHD As Integer)
        'Load Chitiet hoa don theo ma hoadon
        Dim sql As String = "select * from CThoadon where mahd = " & _maHD
        gcHoadon.DataSource = DataProvider.LoadTable(sql)
    End Sub
    Function LayMaHDTheoBanSelect(ByVal _maBan As Integer)
        Dim _maHD As Integer = 0
        Dim sql As String = "select MaHD from HOADON h,BANAN b where h.tinhtrang=0 and b.MaBan = h.MaBan and b.MaBan = " & _maBan
        _maHD = Convert.ToInt16(DataProvider.ExecuteScalar(sql))
        Return _maHD
    End Function
    Private Sub gvBanan_SelectionChanged(sender As Object, e As EventArgs) Handles gvBanan.SelectionChanged
        txtTenkh.Text = ""
        getDataInGvBanAn()
        Dim sqltthoadon As String = "select Tinhtrang from hoadon where tinhtrang = 0 and maban = " & tb.MaBan
        hd.Tinhtrang = Convert.ToInt16(DataProvider.ExecuteScalar(sqltthoadon))
        If tb.MaTT = 2 And hd.Tinhtrang = 0 Then
            txtTenBan.Text = tb.TenBan
            hd.MaHD = LayMaHDTheoBanSelect(tb.MaBan)
            loadCTHoaDonTheoMaHD(hd.MaHD)
            getTenKHtheoBan()
        Else
            txtTenBan.Text = ""
            loadCTHoaDonTheoMaHD(-1)
        End If
        If tb.MaTT = 2 Then
            btEnable()
        Else
            btDisable
        End If
    End Sub
    Sub loadMonAn()
        Dim sql As String = "select MaMA,TenMA,DonGia from MONAN"
        gcMon.DataSource = DataProvider.LoadTable(sql)
    End Sub

    Private Sub btnThemDV_Click(sender As Object, e As EventArgs) Handles btnThemDV.Click
        Dim f As New F_MonAn
        f.ShowDialog
        loadMonAn()
    End Sub
    Sub add_Item()
        If txtSoluong.Text = "" Or Convert.ToInt16(txtSoluong.Text) <= 0 Then
            MsgBox("Bạn đã nhập sai số lượng!")
            Return
        End If
        GetDataInGvMonAn()
        cthd.MaMA = monan.MaMA
        cthd.TenMA = monan.TenMA
        cthd.MaHD = LayMaHDTheoBanSelect(tb.MaBan)
        cthd.DonGia = Convert.ToInt16(monan.DonGia)
        cthd.SoLuong = Convert.ToInt16(txtSoluong.Text)
        cthd.ThanhTien = Convert.ToDouble(cthd.SoLuong * cthd.DonGia)
        BUSCTHoaDOn.CTHoaDOn_Insert(cthd)
        loadCTHoaDonTheoMaHD(cthd.MaHD)
        txtSoluong.Value = 1
    End Sub
    Private Sub btnAdd_Click(sender As Object, e As EventArgs) Handles btnAdd.Click
        'Cai lenh nay co nghia la them MonAn vao HoaDon tuong ung
        add_Item()
    End Sub
    Sub GetDataInGvMonAn()
        monan.MaMA = gvMonAn.GetFocusedRowCellDisplayText("MaMA")
        monan.TenMA = gvMonAn.GetFocusedRowCellDisplayText("TenMA")
        monan.DonGia = gvMonAn.GetFocusedRowCellDisplayText("DonGia")
    End Sub
    Sub getTongCong()

    End Sub
    Sub GetDataIngcCTHoaDon()
        cthd.MaMA = gvHoadon.GetFocusedRowCellDisplayText("MaMA")
        cthd.TenMA = gvHoadon.GetFocusedRowCellDisplayText("TenMA")
        cthd.DonGia = gvHoadon.GetFocusedRowCellDisplayText("DonGia")
        cthd.SoLuong = gvHoadon.GetFocusedRowCellDisplayText("SoLuong")
        cthd.MaCTHD = gvHoadon.GetFocusedRowCellDisplayText("MaCTHD")
        cthd.MaHD = gvHoadon.GetFocusedRowCellDisplayText("MaHD")
    End Sub

    Private Sub btnRemove_Click(sender As Object, e As EventArgs) Handles btnRemove.Click
        GetDataIngcCTHoaDon()
        BUSCTHoaDOn.CTHoaDOn_Delete(cthd)
        loadCTHoaDonTheoMaHD(hd.MaHD)
    End Sub
    Sub getTenKHtheoBan()
        Dim sql As String = "select k.TenKH from BANAN b, KHACHHANG k, HOADON h where b.MaTT =2 and h.TinhTrang =0 and k.MaKH = h.MaKH and b.MaBan = h.MaBan and b.MaBan =" & tb.MaBan
            txtTenkh.Text = DataProvider.ExecuteScalar(sql)
    End Sub
    Private Sub btnEdit_Click(sender As Object, e As EventArgs) Handles btnEdit.Click
        If txtSoluong.Text = "" Or Convert.ToInt16(txtSoluong.Text) <= 0 Then
            MsgBox("Bạn đã nhập sai số lượng!")
            Return
        End If
        GetDataIngcCTHoaDon()
        cthd.SoLuong = txtSoluong.Value
        cthd.ThanhTien = cthd.SoLuong * cthd.DonGia
        'cap nhat so luong va thanh tien
        Dim sql As String = String.Format("update CTHOADON set ThanhTien='{1}',soluong={0} where MaCTHD = '{2}'", cthd.SoLuong, cthd.ThanhTien, cthd.MaCTHD)
        DataProvider.ExecuteNonQuery(sql)
        loadCTHoaDonTheoMaHD(hd.MaHD)
        txtSoluong.Value = 1
    End Sub
    Sub btEnable()
        btnThanhtoan.Enabled = True
        btnAdd.Enabled = True
        btnEdit.Enabled = True
        btnRemove.Enabled = True
        btnHuyHoaDOn.Enabled = True
        btnThemDV.Enabled = True
        btnChuyenBan.Enabled = True
        btnGopban.Enabled = True
    End Sub
    Sub btDisable()
        btnThanhtoan.Enabled = False
        btnAdd.Enabled = False
        btnEdit.Enabled = False
        btnRemove.Enabled = False
        btnHuyHoaDOn.Enabled = False
        btnThemDV.Enabled = False
        btnChuyenBan.Enabled = False
        btnGopban.Enabled = False
    End Sub

    Private Sub gvMonAn_DoubleClick(sender As Object, e As EventArgs) Handles gvMonAn.DoubleClick
        If tb.MaTT = 2 Then
            add_Item()
        End If
    End Sub
    Sub sendDataToFormChuyenBan()
        getDataInGvBanAn()
        Dim f As New F_ChuyenBan
        f.receiver(tb)
        f.ShowDialog
    End Sub

    Private Sub SimpleButton1_Click(sender As Object, e As EventArgs) Handles btnChuyenBan.Click
        sendDataToFormChuyenBan()
        loadBanAn
    End Sub

    Private Sub SimpleButton2_Click(sender As Object, e As EventArgs) Handles btnGopban.Click
        getDataInGvBanAn()
        Dim f As New F_GopBan
        f.receiver(tb)
        f.ShowDialog()
    End Sub
End Class
